﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.ThrongIn.BLL;
using Com.ThrongIn.BE;
using System.Web.Services;
using Com.ThrongIn.Utilities;
using System.Web.Script.Serialization;
namespace ThrongIn
{
    public partial class ProfileDetail : BasePage
    {
        #region properties
        protected Profile ProfileToDisplay{get;set;}
        protected string ImageBaseUrl{get {return ConfigurationUtility.ImageBaseUrl;}}
        protected Int64 ProfileId 
        { 
            get; set; 
        }
        protected Int64 FileId
        {
            get;
            set;
        }
        #endregion

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ProfileId = -1;
                if (Request.QueryString["pid"] != null)
                {
                    Int64 pid = -1;
                    if (Int64.TryParse(Convert.ToString(Request.QueryString["pid"]), out pid))
                    {
                        ProfileId = pid;  
                    }
                }
                if (Request.QueryString["fid"] != null)
                {
                    Int64 fid = -1;
                    if (Int64.TryParse(Convert.ToString(Request.QueryString["fid"]), out fid))
                    {
                        FileId = fid;                    
                    }
                    File file = GetFileByType(FileId);

                    if (file != null && file.Entity_Type.Equals("IMG", StringComparison.CurrentCultureIgnoreCase))
                    {
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "carousel", " $(document).ready(function() {GetImageCarousel('" + ProfileId + "');$(window).load(function(){$('#" + FileId + "').trigger('click');});});", true);
                    }
                    else if (file != null && file.Entity_Type.Equals("VID", StringComparison.CurrentCultureIgnoreCase))
                    {
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "carousel", " $(document).ready(function() {GetVidCarousel('" + ProfileId + "');$(window).load(function(){$('#" + FileId + "').trigger('click');});});", true);
                    }
                   
                }
                ProfileToDisplay = (new ProfileBLL()).GetProfileDetail(ProfileId);
                if (ProfileToDisplay != null)
                {
                    if (ProfileToDisplay.Profile_Image  == null)
                    {
                        imgProfile.Src = ResolveUrl(String.Format("~/Handler/ImageHandler.ashx?image={0}&width=170&height=190", ResolveClientUrl("Images/No-Image-Available.gif")));
                    }
                    else
                    {
                        byte[] afterResize = ImageResizer.ByteFromBitmap(ImageResizer.ResizeImage(ImageResizer.BitmapFromByteArray(ProfileToDisplay.Profile_Image), 170, 190, ImageResizer.ResizeOptions.MaxWidthAndHeight));

                        imgProfile.Src = "data:image/jpg;base64," + Convert.ToBase64String(afterResize);
                    }
                }
                BindRepeater();
            }
        }

        protected void rptGroup_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Int64 groupId = Convert.ToInt64(DataBinder.Eval(e.Item.DataItem,"Attribute_Group_Id"));
                List<Com.ThrongIn.BE.Attribute> attributes = new List<Com.ThrongIn.BE.Attribute>();
                attributes = ProfileToDisplay.AttributeList.FindAll(x => x.AttributeGroup.Attribute_Group_Id == groupId);
                Repeater rptAttr = e.Item.FindControl("rptAttribute") as Repeater;
                rptAttr.DataSource = attributes;
                rptAttr.DataBind();
            }
        }

        #endregion

        #region Web Methods

        [WebMethod]
        public static List<File> LoadFiles(Int64 profileId, int typeId)
        {
            List<File> files = new ProfileBLL().GetProfileFiles(profileId, typeId);
            return (files);            
        }
        [WebMethod]
        public static List<FileComment> LoadFileComments(Int64 fileId)
        {
            List<FileComment> comments = new ProfileBLL().GetFileComments(fileId,-1);
            return (comments);
        }

        
        
        [WebMethod]
        public static FileComment AddFileComments(Int64 fileId, string comment)
        {
            if (HttpContext.Current.Session["USER"] != null)
            {
                FileComment fileComment = new FileComment() { File_Id = fileId, File_Comment = HttpUtility.HtmlEncode(comment) };
                fileComment.User.Id = ((User)HttpContext.Current.Session["USER"]).Id;
                Int64 id =  new ProfileBLL().AddFileComments(fileComment);
                return GetFileComments(id);
              
            }
            else
                return null;
        }

        [WebMethod]
        public static List<ProfileComment> LoadProfileComments(Int64 profileId)
        {
            List<ProfileComment> comments = new ProfileBLL().GetProfileComments(profileId, -1);
            return (comments);
        }

        [WebMethod]
        public static ProfileComment  AddProfileComments(Int64 profileId, string comment)
        {
            if (HttpContext.Current.Session["USER"] != null)
            {
                ProfileComment profileComment = new ProfileComment() { Profile_Id = profileId, Profile_Comment =  HttpUtility.HtmlEncode(comment) };
                profileComment.User.Id = ((User)HttpContext.Current.Session["USER"]).Id;
                Int64 id = new ProfileBLL().AddProfileComments(profileComment);
                return GetProfileComments(id);

            }
            else
                return null;
        }

        #endregion

        #region Private members
        /// <summary>
        /// bind repeater
        /// </summary>
        private void BindRepeater()
        {
            rptGroup.DataSource = GetDistinctGroup();
            rptGroup.DataBind();
        }
        /// <summary>
        /// Get distinct group
        /// </summary>
        /// <returns></returns>
        private List<AttributeGroup> GetDistinctGroup()
        {
            if (ProfileToDisplay != null)
            {
                List<AttributeGroup> groups = new List<AttributeGroup>();
                foreach (Com.ThrongIn.BE.Attribute attr in ProfileToDisplay.AttributeList)
                {
                    if (groups.Find(x => x.Attribute_Group_Id == attr.AttributeGroup.Attribute_Group_Id) == null)
                        groups.Add(attr.AttributeGroup);
                }
                return groups;
            }
            else
                return null;
        }
        /// <summary>
        /// Get file comment based on comment id
        /// </summary>
        /// <param name="commentId"></param>
        /// <returns></returns>
        private static FileComment GetFileComments(Int64 commentId)
        {
            List<FileComment> comments = new ProfileBLL().GetFileComments(-1, commentId);
            return (comments.First());
        }
        /// <summary>
        /// Get profile comment based on comment id
        /// </summary>
        /// <param name="commentId"></param>
        /// <returns></returns>
        private static ProfileComment GetProfileComments(Int64 commentId)
        {
            List<ProfileComment> comments = new ProfileBLL().GetProfileComments(-1, commentId);
            return (comments.First());
        }

        private File GetFileByType(Int64 fileId)
        {
            return new ProfileBLL().GetFileByType(fileId);
        }
        #endregion        

    }
}
